<template>
  <div class="login">

    <van-form @submit="login">
      <van-field
        v-model="user.phone"
        name="帐号"
        label="帐号"
        placeholder="帐号"
        :rules="[{ required: true, message: '请填写帐号' }]"
      />
      <van-field
        v-model="user.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="primary">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { reqlogin } from "../../http/http";

export default {
  data() {
    return {
      user: {
        phone: "",
        password: ""
      }
    };
  },
  methods: {
    login() {
      reqlogin(this.user).then(res => {
        console.log("交互成功");
        console.log(res);
        if (res.data.code === 200) {
          //登录成功
          // ①弹成功
          alert(res.data.msg);

          // ②存判断是否登录的标识,而且这个标识我们存了用户的信息
          localStorage.setItem("isLogin", JSON.stringify(res.data.list));

          // ③ 跳转到首页
          this.$router.push("/index/home");
        } else {
          //登录失败
          alert(res.data.msg);
        }
      });
    }
  }
};
</script>

<style scoped>
.login {
  margin: 50px auto;
}
h1 {
  margin: 100px 0;
  text-align: center;
}
.box {
  margin: 30px 40px;
  width: 300px;
}
.btn {
  background: #ccc;
  width: 40px;
  line-height: 20px;
  height: 20px;
  color: white;
  margin-left: 150px;
}
.box-btn {
  margin-left: 10px;
}
.phone,
.yanz {
  width: 195px;
}
</style>